package com.lntu.meeting.service.impl;

import com.lntu.meeting.service.ManageService;
import com.lntu.meeting.pojo.MeetingRoom;
import com.lntu.meeting.pojo.Notes;
import com.lntu.meeting.mapper.ManageMapper;
import com.lntu.meeting.service.ManageService;
import com.lntu.meeting.service.ex.DeleteException;
import com.lntu.meeting.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ManageServiceImpl implements ManageService {

  /*  @Resource
    private ManageService manageService;*/

    @Autowired
    private ManageMapper manageMapper;


    /**
     * 用户删除自己的会议室申请
     */
    public Integer deleteNotesById(Integer userid,Integer id){
        Integer result=manageMapper.findUseridByid(id);
        if (userid!=result){
            throw new DeleteException("用户只能删除自己创建的申请");
        }
        Integer integer=manageMapper.delete(id);
        return integer;
    }

    /**
     * 管理员通过申请id批准预定信息
     */
    public Integer applyById(Integer id){
        Integer result=manageMapper.applyById(id);
        return result;
    }

    /**
     * 管理员查询所有会议室预定信息
     */
    @Override
    public List<Pager> findAll(Integer page, Integer size, Date date) {
        /**传递分页初始页0和长度*/
        Map<String, Object> params = new HashMap<String, Object>();
        /**查询每页申请数据Notes*/
        List<Notes> list = new ArrayList<Notes>();
        /**返回所有分页数据*/
        List<Pager> pager = new ArrayList<Pager>();
        int i = 0;
        Date startDate = date;
        while ((page - 1) * size < manageMapper.count()) {
            params.put("page", (page - 1) * size);
            params.put("size", size);
            params.put("startDate", startDate);
            //查询每页申请数据Notes
            list = manageMapper.findAll(params);
            System.out.println("list=" + list);
            //返回所有分页数据
            Pager pager1 = new Pager();
            pager1.setRows(list);
            pager1.setTotal(manageMapper.count());
            pager.add(pager1);
            page++;
        }
         /*while((page-1)*size<manageMapper.count()){

         *查询每页申请数据Notes
            list=manageMapper.findAll(startDate,(page-1)*size,size);
            System.out.println("list="+list);
            *返回所有分页数据
            Pager pager1=new Pager();
            pager1.setRows(list);
            pager1.setTotal(manageMapper.count());
            pager.add(pager1);
            page++;
        }*/
        return pager;
    }

    /**
     * 管理员根据用户名或会议开始、结束时间查询会议室预定信息
     */
    public List<Notes> find(Integer id,Integer starttime,Integer endtime){
        List<Notes> result=manageMapper.find(id,starttime,endtime);
        return result;
    }

    /**
     * 查询当前所有会议室
     */
    @Override
    public List<MeetingRoom> findAllRoom() {
        List<MeetingRoom> list=manageMapper.getAllRoom();
        return list;
    }
}
